package com.ruoyi.project.manhua.comment.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.redis.RedisKey;
import com.ruoyi.framework.redis.RedisUtils;
import com.ruoyi.project.manhua.name.domain.MhName;
import com.ruoyi.project.manhua.newXml.MhCommentNewMapper;
import com.ruoyi.project.manhua.newXml.MhNameNewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.project.manhua.comment.domain.MhComment;
import com.ruoyi.project.manhua.comment.service.IMhCommentService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 漫画评论
 * Service业务层处理
 *
 * @author yuan
 * @date 2021-01-27
 */
@Service
public class MhCommentServiceImpl implements IMhCommentService {
    @Autowired
    private MhNameNewMapper mhNameNewMapper;
    @Autowired
    private MhCommentNewMapper mhCommentMapper;

    @Autowired
    private RedisUtils redisUtils;

    /*启用redis开关*/
    @Value("${spring.redis.enabled}")
    private boolean enabled;


    /**
     * 查询漫画评论
     *
     * @param id 漫画评论
     *           ID
     * @return 漫画评论
     */
    @Override
    public MhComment selectMhCommentById(Long id) {

        MhComment mhComment = null;

        if (enabled) {

            mhComment = (MhComment) redisUtils.get(RedisKey.MhCommentRedis.name_str_id.getKey() + id);

            if (ObjectUtils.isEmpty(mhComment)) {
                /*创建锁*/
                Lock lock = new ReentrantLock();
                /*获取锁*/
                lock.lock();

                mhComment = (MhComment) redisUtils.get(RedisKey.MhCommentRedis.name_str_id.getKey() + id);

                if (ObjectUtils.isEmpty(mhComment)) {

                    mhComment = mhCommentMapper.selectMhCommentById(id);
                    /*储存redis中*/
                    redisUtils.set(RedisKey.MhCommentRedis.name_str_id.getKey() + id, mhComment, RedisKey.time);
                    /*释放锁*/
                    lock.unlock();
                }

            }

        } else {

            mhComment = mhCommentMapper.selectMhCommentById(id);
        }


        return mhComment;
    }

    /**
     * 查询漫画评论
     * 列表
     *
     * @param mhComment 漫画评论
     * @return 漫画评论
     */
    @Override
    public List<MhComment> selectMhCommentList(MhComment mhComment) {
        return mhCommentMapper.selectMhCommentList(mhComment);
    }

    /**
     * 新增漫画评论
     *
     * @param mhComment 漫画评论
     * @return 结果
     */
    @Override
    public int insertMhComment(MhComment mhComment) {
        mhComment.setCreateTime(DateUtils.getNowDate());
        return mhCommentMapper.insertMhComment(mhComment);
    }

    /**
     * 修改漫画评论
     *
     * @param mhComment 漫画评论
     * @return 结果
     */
    @Override
    public int updateMhComment(MhComment mhComment) {
        mhComment.setUpdateTime(DateUtils.getNowDate());
        return mhCommentMapper.updateMhComment(mhComment);
    }

    /**
     * 删除漫画评论
     * 对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMhCommentByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        Arrays.stream(array).forEach((e) -> {
            deleteMhCommentById(Long.valueOf(e));
        });
        return 1;
    }

    /**
     * 删除漫画评论
     * 信息
     *
     * @param id 漫画评论
     *           ID
     * @return 结果
     */
    @Override
    public int deleteMhCommentById(Long id) {
        MhComment mhComment = mhCommentMapper.selectMhCommentById(id);
        MhName mhNames = mhNameNewMapper.selectMhNameById(mhComment.getNameId());
        Integer give = mhNames.getGiveLike();
        MhName mhName = new MhName();
        mhName.setId(id);
        mhName.setGiveLike(--give);
        mhNameNewMapper.updateMhName(mhName);
        redisUtils.del(RedisKey.MhCommentRedis.name_str_id.getKey() + id);
        redisUtils.del(RedisKey.mhName.name_id.getKey() + mhName.getId());
        return mhCommentMapper.deleteMhCommentById(id);
    }
}
